package com.net.xpay.alipay.common;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class CommonCoreConfig {

    @Autowired
    private CommonAlipayProperties commonAlipayProperties;

    /**
     * 系统自用收款用的
     */
    @Bean
    public CommonAlipayClient commonAlipayClient() throws AlipayApiException {
        String url = commonAlipayProperties.getAlipayTopUrl();
        String appId = commonAlipayProperties.getAlipayAppId();
        String appPrivateKey = commonAlipayProperties.getAppPrivateKey();
        String alipayPublicKey = commonAlipayProperties.getAlipayPublicKey();

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setPrivateKey(appPrivateKey);
        alipayConfig.setAppId(appId);
        alipayConfig.setServerUrl(url);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setFormat("json");

        DefaultAlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        return new CommonAlipayClient(alipayClient, commonAlipayProperties);
    }
}
